﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using HRProject.Code.DAO;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;

namespace HRProject.User
{
    public partial class ManageUser2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DepartmentDAO departmentDao = new DepartmentDAO();
                BLDepartment filter = new BLDepartment();
                filter.Id = Guid.Empty;
                filter.Name = string.Empty;
                filter.Parent = new BLDepartment();
                filter.Parent.Id = Guid.Empty;
                List<BLDepartment> depts = departmentDao.search(filter);
                depts.Insert(0, filter);
                ddlDepartment.DataSource = depts;
                ddlDepartment.DataBind();

                dgvUsers.EmptyDataText = ContentProvider.GetValue("User_NoData");
            }
        }

        protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Guid deptId = new Guid(ddlDepartment.SelectedValue);
                List<BLRole> roles = new List<BLRole>();
                BLRole filter = new BLRole();
                filter.Id = Guid.Empty;
                filter.Name = string.Empty;
                filter.Department = new BLDepartment();
                filter.Department.Id = deptId;
                if (!deptId.Equals(Guid.Empty))
                {
                    RoleDAO roleDao = new RoleDAO();
                    roles = roleDao.searchRolesInDepartment(filter);
                }
                roles.Insert(0, filter);
                ddlRole.DataSource = roles;
                ddlRole.DataBind();

                loadUsers();
            }
            catch (Exception exp)
            {
                Console.Write(exp.StackTrace);
            }
        }

        private void loadUsers()
        {
            try
            {
                string roleName = ddlRole.SelectedItem.Text;
                List<BLUser> users = new List<BLUser>();
                if (!string.IsNullOrEmpty(roleName))
                {
                    string[] usernames = Roles.GetUsersInRole(roleName);
                    foreach (string un in usernames)
                    {
                        BLUser user = new BLUser(Membership.GetUser(un));
                        users.Add(user);
                    }
                }
                dgvUsers.DataSource = users;
                dgvUsers.DataBind();
            }
            catch (Exception exp)
            {
                Console.Write(exp.StackTrace);
            }
        }

        protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadUsers();
        }

        protected void dgvUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dgvUsers.PageIndex = e.NewPageIndex;
            loadUsers();
        }
    }
}
